importClass('rapi.rbase.RDiv');
importClass('rapi.rwidget.rfloatdialog.RFloatDialog');
importClass('rapi.rbase.RImage');

function RLabelPoint(latlng,zIndex,icon,iconlabel,xoffset,yoffset,label,detail){
	this.type='LabelPoint';
	if(!latlng){return;}
	this.latlng=latlng;
	this.lat=this.latlng.lat;
	this.lng=this.latlng.lng;
	if(!icon){return;}
	this.icon=icon;
	if(!iconlabel){return;}
	this.iconlabel=iconlabel;
	if(zIndex){
		this.zIndex=zIndex;
	}else{
		this.zIndex=0;
	}
	if(xoffset){
		this.xoffset=xoffset;
	}else{
		this.xoffset=0;
	}
	if(yoffset){
		this.yoffset=yoffset;
	}else{
		this.yoffset=0;
	}
	this.label=label;
	this.detail=detail;
	
	this.div=new RDiv(0,0,null,null).get();
	
	this.style=this.div.style;
	this.style.zIndex=this.zIndex;
	this.style.fontSize = '12px';
	
	this.labelImg=new RImage(xoffset,yoffset,this.iconlabel).get();
	
	this.img=new RImage(0,0,this.icon).get();
	
	this.img.style.zIndex=zIndex;
	this.img.onload=function(){
		this.style.left=-this.width/2;
		this.style.top=-this.height/2;
	}
	this.img.style.left=-this.img.width/2;
	this.img.style.top=-this.img.height/2;
	
	this.div.appendChild(this.img);
	this.div.appendChild(this.labelImg);
	
	if(this.detail){
	    this.div.detail=new RFloatDialog(20,10,this.label.length*20,this.label,this.detail);
	    this.div.detail.get().onmouseover=function(){
	      	this.depth=this.parentNode.style.zIndex;
	      	this.parentNode.style.zIndex=10000;
	    } 
	    this.div.detail.get().onmouseout=function(){
	      	this.parentNode.style.zIndex=this.depth;
	    }
	    this.div.detail.style.zIndex=10000;
	    this.div.detail.hide();
	    this.div.appendChild(this.div.detail.get());
	}
	this.img.onmouseover=function(){
		this.pin=false;
		if(this.parentNode.style.zIndex!=10000){
			this.depth=this.parentNode.style.zIndex;
		}
		this.parentNode.style.zIndex=10000;
		if(!this.parentNode.detail.get().pin){
			this.parentNode.detail.get().style.left=20;
			this.parentNode.detail.get().style.top=10;
		}
		this.parentNode.detail.show();
	}
	this.img.onclick=function(){
		this.pin=true;
		this.parentNode.detail.get().pin=true;
	}
	this.img.onmouseout=function(){
		if(!this.pin&&!this.parentNode.detail.get().pin){
			this.parentNode.style.zIndex=this.depth;
			this.parentNode.detail.hide();
		}
	}
	
	this.toString=function(){
		return 'RLabelPoint';
	} 
	this.get=function(){
		return this.div;
	}
	
	this.showDetail=function(){
		if(this.detail){
			this.pin=true;
			this.div.detail.get().pin=true;
			this.div.detail.show();
		}
	}
	
	this.hideDetail=function(){
		if(this.detail){
			this.pin=false;
			this.div.detail.get().pin=false;
			this.div.detail.hide();
		}
	}
	
	this.destroy=function(){
	    if(this.labelImg){
	        this.labelImg=null;
	    }
	    if(this.img){
	        this.img=null;
	    }
	    if(this.div.detail){
	        this.div.detail=null;
	    }
		if(this.div.parentNode){
			this.div.parentNode.removeChild(this.div);
		} 
		this.div=null;
		this.toString=function(){return null;};
	}
}